package com.hqyj.fj.javaSpringBoot.modules.account.controller;

import com.github.pagehelper.PageInfo;
import com.hqyj.fj.javaSpringBoot.modules.account.entity.Role;
import com.hqyj.fj.javaSpringBoot.modules.common.entity.Result;
import com.hqyj.fj.javaSpringBoot.modules.account.service.RoleService;
import com.hqyj.fj.javaSpringBoot.modules.common.entity.SearchBean;
import com.hqyj.fj.javaSpringBoot.modules.test.entity.City;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/api")
public class RoleController {
    @Autowired
    RoleService roleService;

    /*
     * 127.0.0.1:8081/api/role --- post
     * {"roleName":"fengjin"}
     */
    @PostMapping(value = "/role", consumes = "application/json")
    public Result<Role> insertRole(@RequestBody Role role){
        return  roleService.insertRole(role);
    }

    /*
     * 127.0.0.1:8081/api/role ---- put
     *{"id":1, "roleName":"普通管理员"}
     */
    @PutMapping(value = "/role", consumes = "application/json")
    public Result<Role> updateCity(@RequestBody Role role) {
        return roleService.updateRole(role);
    }

    /*
     * 127.0.0.1:8081/api/role/12 ---- delete
     */
    @DeleteMapping(value = "/role/{id}")
    public Result<Role> deleteRole(@PathVariable  int id){
        return  roleService.deleteRole(id);
    }

    /*
     * 127.0.0.1:8081/api/role/1 ---- get
     */
    @GetMapping(value = "/role/{id}")
    public Role getRoleById(@PathVariable int id) {
        return roleService.getRoleById(id);
    }

    /*
     * 127.0.0.1:8081/api/roles ---- post
     * {"currentPage":1,"pageSize":5,"orderBy":"city_name","direction":"desc","keyword":""}
     */

    /*
     * 127.0.0.1:8081/api/roles ---- get
     */
    @GetMapping("/roles")
    public List<Role> getRoles() {
        return roleService.getRoles();
    }

    @PostMapping(value = "/roles", consumes = "application/json")
    public PageInfo<Role> getCitiesBySearchBean(@RequestBody SearchBean searchBean) {
        return roleService.getRolesBySearchBean(searchBean);
    }

    /*
     * 127.0.0.1:8081/api/batch/roles ---- post
     * [{"roleName":"超级管理"},
     * {"roleName":"普通用户"}]
     * */
    @PostMapping(value = "/batch/roles",consumes = "application/json")
    public Result<Object> insertCities(@RequestBody List<Role> roles){
        return roleService.insertRoles(roles);
    }


    /*
    * /*
     * 127.0.0.1:8081/api/roles/20 ---- get
     */
//Required URI template variable 'userId' for method parameter type int is not present
    @GetMapping(value = "/roles/{userId}")
    public List<Role> getRolesByUserId(@PathVariable  int userId){
        return  roleService.getRolesByUserId(userId);

    }
}
